package com.liser.print.bluetooth.receiver;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.liser.print.bluetooth.callback.BaseCallback;

/**
 * 描述：监听蓝牙广播
 *
 * @author zhangqin
 * @date 2018/5/30
 */
public class BtSwitchReceiver extends BroadcastReceiver {

    private BaseCallback mBaseCallback;

    public BtSwitchReceiver(Context context, BaseCallback baseCallback) {
        mBaseCallback = baseCallback;
        IntentFilter filter = new IntentFilter();
        // 蓝牙开关状态
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);

        context.registerReceiver(this, filter);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        int state;
        if (action == null) {
            return;
        }
        // 蓝牙开关状态
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
            mBaseCallback.onStateSwitch(state);
        }
    }
}